﻿<?php
            
            $pagesize=3;
            $pagenum=2;
	        $page=!empty($_GET['page'])?$_GET['page']:1;            
            $obj=new mysql($config);
			$show=getPage($obj,'news',$page,$pagesize,$pagenum);
            
			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='page'){
                
				echo json_encode($show);   
                die;
			}


			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='del'){
                       $id=$_GET['id'];
                       if(!empty($id)){
		                       $row=$obj->delete($id,'news');
		                       echo $row;
		                       die;	
                       }else{
		                       echo 0;
		                       die;
                       }
			
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='look'){
				$id=$_GET['id'];
                for ($i=0; $i <count($id); $i++) { 
                	$row[$id[$i]]=$obj->update(array('statue'=>1),array('id'=>$id[$i],'statue'=>0),'news');
                }
                echo json_encode($row);
				die;

			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='addtr'){
                $sql="select * from `cate` where `pid`=0";
                $date=$obj->getCondition($sql);
                echo json_encode($date);
                die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='cate'){
                $id=$_GET['id'];
                $sql="select * from `cate` where `pid`=$id";
                $date=$obj->getCondition($sql);
                echo json_encode($date);
                die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='add'){
				$_POST['createtime']=time();
				$row=$obj->insert($_POST,'news');
				echo $row;
				die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='catelist'){
                $id=$_GET['id'];
                

                function get(){
                	static $arr=array();
	                $sql="select * from `cate` id=$id";
	                $date=$obj->getOne($sql);
	                $sql2="select * from `cate` pid=$date[pid]";
	                $arr[]=$obj->getCondition($sql2);                	
                }

				die;
			}




			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='del'){
                       $id=$_GET['id'];
                       if(!empty($id)){
		                       $row=$obj->delete($id,'news');
		                       echo $row;
		                       die;	
                       }else{
		                       echo 0;
		                       die;
                       }
			
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='look'){
				$id=$_GET['id'];
                for ($i=0; $i <count($id); $i++) { 
                	$row[$id[$i]]=$obj->update(array('statue'=>1),array('id'=>$id[$i],'statue'=>0),'news');
                }
                echo json_encode($row);
				die;

			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='addtr'){
                $sql="select * from `cate` where `pid`=0";
                $date=$obj->getCondition($sql);
                echo json_encode($date);
                die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='cate'){
                $id=$_GET['id'];
                $sql="select * from `cate` where `pid`=$id";
                $date=$obj->getCondition($sql);
                echo json_encode($date);
                die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='add'){
				$_POST['createtime']=time();
				$row=$obj->insert($_POST,'news');
				echo $row;
				die;
			}

			if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])&&$_GET['flag']=='catelist'){
                $id=$_GET['id'];
                 

                function get($id,$obj){
                	static $arr=array();

	                $sql="select * from `cate` where id=$id";
	                $date=$obj->getOne($sql);

	                if($id!=0){
			                $sql2="select * from `cate` where pid=$date[pid]";
			                $arr[$id]=$obj->getCondition($sql2);
			                $arr=get($date['pid'],$obj);
	                
	                }
					return $arr;

                }
                // print_r(get($id,$obj));die;
				echo json_encode(get($id,$obj));
				die;
			}




            require_once (VIEW_PATH.'admin/layout.html');